﻿package com.taobao.top
{
	import com.adobe.crypto.MD5;
	
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class TOPURLLoader extends URLLoader
	{
		public function TOPURLLoader(request:URLRequest=null)
		{
			super(request);
		}
		/**
		 *  
		 * @param paramsArray
		 * 
		 */		
		public function loadData(paramsArray:Array):void
		{
			paramsArray.push(new TopRequestParam("app_key",TOPConfig.getInstance().appKey));
			paramsArray.push(new TopRequestParam("v","1.0"));
			if(TOPConfig.getInstance().sessionKey!=null && TOPConfig.getInstance().sessionKey!="")
			{
				paramsArray.push(new TopRequestParam("session",TOPConfig.getInstance().sessionKey));
			}
			paramsArray.push(new TopRequestParam("format","json"));
			paramsArray.push(new TopRequestParam("timestamp",TOPUtil.getCustomDateString(new Date())));
			
			paramsArray.sortOn("Key",Array.CASEINSENSITIVE);
			var str:String = TOPConfig.getInstance().appSecret;
			for each(var reqParam:TopRequestParam in paramsArray)
			{
				str+=reqParam.Key+reqParam.Value;
			}
			var sign:String = MD5.hash(encodeURIComponent(str)).toUpperCase();
			
			var requestPty:String="";
			for each(reqParam in paramsArray)
			{
				requestPty+=reqParam.Key+"="+reqParam.Value+"&";
			}
			requestPty+="sign="+sign;
			var urlReq:URLRequest = new URLRequest(TOPConfig.getInstance().ReqURL);
			urlReq.method = "post";
			urlReq.data = requestPty;
			trace(requestPty);
			load(urlReq);
		}
	}
}